import request from '@/utils/request'
import { getToken } from '@/utils/auth'

const bURL = 'http://10.61.22.44:8889/brother_hood/backstage/public/goods/'
const token = getToken()

/*
  获取所有商品属性
 */
export function getSku() {
  return request({
    baseURL: bURL,
    url: 'getSkuKeyVal',
    method: 'get'
  })
}

/*
  分页获取商品信息
 */
export function getGoods(page, pageSize, type) {
  return request({
    baseURL: bURL,
    url: 'ggfa',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'get',
    params: {
      'page': page,
      'pageSize': pageSize,
      'type': type
    }
  })
}

/*
  通过关键词搜索商品
 */
export function searchGoods(page, pageSize, key) {
  return request({
    baseURL: bURL,
    url: 'searchGoods',
    method: 'get',
    params: {
      page, pageSize, key
    }
  })
}

/*
  新增商品，通过 FormData 提交照片和普通数据
 */
export function addGoods(addGoodsForm, introducePic) {
  let formData = new FormData()
  for (let i in addGoodsForm) {
    formData.append(i, addGoodsForm[i])
  }
  introducePic.forEach((e) => {
    formData.append('introducePic[]', e)
  })
  return request({
    baseURL: bURL,
    url: 'addGoods',
    headers: {
      'Authorization': 'Bearer ' + token,
      'Content-Type': 'multipart/form-data'
    },
    method: 'post',
    data: formData
  })
}

/*
  更新商品基本信息
 */
export function updGoods(updGoodsForm) {
  return request({
    baseURL: bURL,
    url: 'updGInfo',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'put',
    params: updGoodsForm
  })
}

/*
  删除商品
 */
export function delGoods(gid) {
  return request({
    baseURL: bURL,
    url: 'delGoods',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'delete',
    params: {
      id: gid
    }
  })
}

/*
  更新标准商品的价格、库存等
 */
export function updSpu(updSpuForm) {
  return request({
    baseURL: bURL,
    url: 'updGSpu',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'put',
    params: updSpuForm
  })
}

/*
  删除某件标准商品
 */
export function delSpu(delId) {
  return request({
    baseURL: bURL,
    url: 'delGSpec',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'delete',
    params: {
      id: delId
    }
  })
}

/*
  添加标准商品
 */
export function addGoodsSpu(addSpuForm) {
  return request({
    baseURL: bURL,
    url: 'addGSpu',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'post',
    params: addSpuForm
  })
}

/*
  添加商品图片
 */
export function addGoodsPic(id, pictures, introducePic) {
  let formData = new FormData()
  formData.append('id', id)
  formData.append('pictures', pictures)
  if (introducePic !== '') {
    introducePic.forEach((e) => {
      formData.append('introducePic[]', e)
    })
  } else {
    formData.append('introducePic', introducePic)
  }
  return request({
    baseURL: bURL,
    url: 'addGPic',
    headers: {
      'Authorization': 'Bearer ' + token,
      'Content-Type': 'multipart/form-data'
    },
    method: 'post',
    data: formData
  })
}

/*
  删除商品图
 */
export function delGoodsPic(pid) {
  return request({
    baseURL: bURL,
    url: 'delGPic',
    headers: {
      'Authorization': 'Bearer ' + token
    },
    method: 'delete',
    params: {
      id: pid
    }
  })
}
